package com.intellagric.service.impl.module.sys_role;

import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.mapper.SysRoleMenuMapper;
import com.intellagric.pojo.SysRoleMenuExample;
import com.intellagric.pojo.SysRoleMenuKey;
import com.intellagric.service.module.sys_role.RoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2018/10/13
 * @DESC： 角色菜单id信息服务实现
 */
@Service
public class RoleMenuServiceImpl implements RoleMenuService {

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    /**
     * 增加角色id和菜单id
     * @param sysRoleMenuKey
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage addRoleMenu(SysRoleMenuKey sysRoleMenuKey) {
        if (sysRoleMenuMapper.insertSelective(sysRoleMenuKey) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 通过角色id删除角色-菜单信息表
     * @param roleId
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage deleteRoleMenuByRoleId(String roleId) {
        SysRoleMenuExample sysRoleMenuExample = new SysRoleMenuExample();
        sysRoleMenuExample.createCriteria().andRoleIdEqualTo(roleId);
        if (sysRoleMenuMapper.deleteByExample(sysRoleMenuExample) != 0) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 修改角色id和菜单id
     * @param sysRoleMenuKey
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage editRoleMenu(SysRoleMenuKey sysRoleMenuKey) {
        SysRoleMenuExample sysRoleMenuExample = new SysRoleMenuExample();
        sysRoleMenuExample.createCriteria().andRoleIdEqualTo(sysRoleMenuKey.getRoleId());
        sysRoleMenuMapper.updateByExample(sysRoleMenuKey,sysRoleMenuExample);
        if (sysRoleMenuMapper.updateByExample(sysRoleMenuKey,sysRoleMenuExample) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 查询权限列表
     * @param roleId
     * @return ResponseMessage
     */
    @Override
    public LayuiDataGridResult getRoleMenuByRoleId(String roleId) {
        SysRoleMenuExample sysRoleMenuExample = new SysRoleMenuExample();
        sysRoleMenuExample.createCriteria().andRoleIdEqualTo(roleId);
        List<SysRoleMenuKey> roleMenuKeys = sysRoleMenuMapper.selectByExample(sysRoleMenuExample);
        return LayuiDataGridResult.success().add(roleMenuKeys,roleMenuKeys.size());
    }
}
